/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_UART_H__
#define __EB_UART_H__

#include <drivers/simple.h>
#include <drivers/platform.h>
#include <kernel/fdt.h>


/**
 * @brief UART device operations
 * @start: uart device start put character
 * @stop: uart device stop put character
 * @put: put character
 * @put_string: put string
 * @get_char: get character
 */
struct uart_ops {
    void (*start)(struct uart_device *pdev);
    void (*stop)(struct uart_device *pdev);
    void (*put_char)(struct uart_device *pdev, char ch);
    void (*put_string)(struct uart_device *pdev, char *str);
    char (*get_char)(struct uart_device *pdev);
    void (*set_baudrate)(struct uart_device *pdev, uint32_t baudrate);
};


/**
 * @brief uart gpio struct
 * @tx: tx pin
 * @rx: rx pin
 */
struct uart_gpio {
    int16_t tx;
    int16_t rx;
};


/**
 * @brief uart master struct
 * @ops: uart operation struct
 * @pins: uart gpio struct
 * @clock_freq_hz: clock frequency in hertz
 */
struct uart_master {
    size_t           io_base;
    struct uart_ops *ops;
    struct uart_gpio pins;
    size_t clock_freq_hz;
};


/**
 * @brief uart_device struct
 * @device: device struct
 * @master: pointer to uart master
 * @baud: baudrate
 * @platform_data: platform data pointer
 */
struct uart_device {
    struct device      device;
    struct uart_master *master;
    uint32_t           baud;
    uint32_t           irq_num;
};


/**
 * @brief uart_driver struct
 * @driver: device driver
 * @probe: probe function
 * @remove: remove function
 * @of_match_table: device tree match table
 */
struct uart_driver {
    struct driver   driver;
    int (*probe)(struct uart_device *dev);
    void (*remove)(struct uart_device *dev);
};


#define  UART_SYSLOG_INFO(...)   SYSLOG_INFO("UART: "__VA_ARGS__)
#define  UART_SYSLOG_ERROR(...)  SYSLOG_ERROR("UART: "__VA_ARGS__)
#define  UART_SYSLOG_WARN(...)   SYSLOG_WARN("UART: "__VA_ARGS__)
#define  UART_DEBUG_TRACE(...)   DEBUG_TRACE("UART: "__VA_ARGS__)


#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief convert driver to uart_driver
 * @param dev point to driver
 * @return uart_driver
 */
static inline struct uart_driver* to_uart_driver(struct driver *drv)
{
    return container_of(drv, struct uart_driver, driver);
}


/**
 * @brief convert device to uart_device
 * @param dev point to device
 * @return uart_device
 */
static inline struct uart_device* to_uart_device(struct device *dev)
{
    return container_of(dev, struct uart_device, device);
}


extern struct bus_type uart_bus;


/**
 * @brief reigister uart device
 * @param dev uart device
 * @return 0 on success, -1 on error
 */
static inline int uart_device_register(struct uart_device *udev)
{
    udev->device.bus = &uart_bus;
    return device_register(&udev->device);
}


/**
 * @brief unregister uart device
 * @param dev uart device
 * @return none
 */
static inline void uart_device_unregister(struct uart_device *udev)
{
    device_unregister(&udev->device);
}


/**
 * @brief register uart driver
 * @param drv uart driver
 * @return 0 on success, -1 on error
 */
static inline int uart_driver_register(struct uart_driver *udrv)
{
    udrv->driver.bus = &uart_bus;
    return driver_register(&udrv->driver);
}


/**
 * @brief unregister uart driver
 * @param drv uart driver
 * @return none
 */
static inline void uart_driver_unregister(struct uart_driver *udrv)
{
    driver_unregister(&udrv->driver);
}


/**
 * @brief set uart driver data
 * @param dev uart device
 * @param data driver data
 * @return none
 */
static inline void uart_set_drvdata(struct uart_device *dev, void *data)
{
    device_set_drvdata(&dev->device, data);
}


/**
 * @brief get uart driver data
 * @param dev uart device
 * @return driver data
 */
static inline void *uart_get_drvdata(struct uart_device *dev)
{
    return device_get_drvdata(&dev->device);
}


/**
 * @brief set uart platform data
 * @param dev uart device
 * @param data platform data
 * @return none
 */
static inline void uart_set_platform_data(struct uart_device *dev, const void *data)
{
    device_set_platform_data(&dev->device, data);
}


/**
 * @brief get uart platform data
 * @param dev uart device
 * @return platform data
 */
static inline const void *uart_get_platform_data(struct uart_device *dev)
{
    return device_get_platform_data(&dev->device);
}


/**
 * @brief put a char to uart
 * @param dev point to uart_device
 * @param ch char to put
 * @return none
 */
static inline void uart_put_char(struct uart_device *dev, char ch)
{
    if(dev == NULL || dev->master == NULL) {
        return ;
    }

    struct uart_master *master = (struct uart_master*)dev->master;
    if(master && master->ops) {
        master->ops->put_char(dev, ch);
    }
}


/**
 * @brief put a string to uart
 * @param dev point to uart_device
 * @param str string to put
 * @return none
 */
static inline void uart_put_string(struct uart_device *dev, char *str)
{
    if(dev == NULL || dev->master == NULL) {
        return ;
    }

    struct uart_master *master = (struct uart_master*)dev->master;
    if(master && master->ops) {
        master->ops->put_string(dev, str);
    }
}


/**
 * @brief get a char from uart
 * @param dev point to uart_device
 * @return char
 */
static inline char uart_get_char(struct uart_device *dev)
{
    if(dev == NULL || dev->master == NULL) {
        return -1;
    }

    struct uart_master *master = (struct uart_master*)dev->master;
    if(master && master->ops) {
        return master->ops->get_char(dev);
    }

    return 0;
}


/**
 * @brief get uart gpio from fdt
 * @param node fdt node
 * @param pin uart gpio
 * @return 0 on success, -1 on error
 * @note: you should add uart pins(tx,rx) into uart master device tree node, for example:
 *        uart1:
 *            ...
 *            tx-gpio: 9
 *            rx-gpio: 10
 *            ...
 */
int uart_master_get_gpio_from_fdt(fdt_node_t *node, struct uart_gpio *pin);


#ifdef __cplusplus
}
#endif


#endif //!__EB_UART_H__
